package org.amuxia.emailtest.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

public class CalendarUtils {
	Calendar calendar=Calendar.getInstance();   
	
	@Test
	public void getTest(){
		calendar.setTime(new Date());
		/*获取今天的日期*/
		System.out.println("今天的日期是："+calendar.get(Calendar.DAY_OF_MONTH));
		/*获取十天之后的日期*/
		calendar.clear();//避免继承当前系统的时间
		calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+10);
		System.out.println("十天之后的日期是："+calendar.get(Calendar.DAY_OF_MONTH));
	}
	/////////////////////////////////////////////
	
	/*计算某一个月的天数是多少*/
	public void maxDay(int year,int month){
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH,month-1);//默认1月为0月
		int day = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		System.out.println(year+"年"+month+"月"+"的最大天数是："+day);
	}
	@Test
	public void maxDayTest(){
		maxDay(2017,9);
	}
	
	///////////////////////////////////////
	public void weekNum(int year,int month,int day){
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH,month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		/*计算某一天是该年的第几个星期*/
		int weekOfYear = calendar.get(Calendar.WEEK_OF_YEAR);
		System.out.println(year+"年"+month+"月"+day+"日是这年中的第"+weekOfYear+"个星期");
		/*计算某一天是该月的第几个星期*/
		int weekOfMonth= calendar.get(Calendar.WEEK_OF_MONTH);
		System.out.println(year+"年"+month+"月"+day+"日是这个月中的第"+weekOfMonth+"个星期");
	}
	@Test
	public void weekNumTest(){
		weekNum(2017, 7, 8);
	}
	//////////////////////////////////////////
	/*计算一年中的第几星期是几号*/
	public void dayNum(int year,int week){
		calendar.clear();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.WEEK_OF_YEAR, week);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		System.out.println(df.format(calendar.getTime()));
	}
	
	@Test
	public void dayNumTest(){
		dayNum(2017,28);
	}
	////////////////////////////////////
	/*add()方法*/
	public void add(int year,int month,int day,int num){
		calendar.clear();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		Date date=calendar.getTime();
		calendar.add(Calendar.DATE, num);
		date=calendar.getTime();
		System.out.println(df.format(date));
	}
	/*使用场景比如，发找回密码邮件，设置一天后过期*/
	@Test
	public void addTest(){
		add(2017,7,10,1);
	}
	//////////////////////////////////
	/*roll()方法*/
	public void roll(int year,int month,int day,int num){
		calendar.clear();
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		Date date=calendar.getTime();
		calendar.roll(Calendar.DATE, num);
		date=calendar.getTime();
		System.out.println(df.format(date));
	}
	/*roll()方法和add()方法用法一样，不过roll()方法是在本月循环，比如，七月三十一号加五天，add()方法结果是八月五号
	 * roll()方法是七月五号，roll()方法用到的少，一般add()使用较多
	 * */
	@Test
	public void rollTest(){
		roll(2017,7,31,5);
	}
	
	/////////////////////////////////////
	/*计算两个任意时间中间相隔的天数*/
	 public int getDaysBetween (Calendar day1, Calendar day2){
	        if (day1.after(day2)){
	            Calendar swap = day1;
	            day1 = day2;
	            day2 = swap;
	        }
	        int days = day2.get(Calendar.DAY_OF_YEAR) - day1.get(Calendar.DAY_OF_YEAR);
	        int y2 = day2.get(Calendar.YEAR);
	        if (day1.get(Calendar.YEAR) != y2){
	            day1 = (Calendar) day1.clone();
	            do{
	                days += day1.getActualMaximum(Calendar.DAY_OF_YEAR);//得到当年的实际天数
	                day1.add(Calendar.YEAR, 1);
	            } while (day1.get(Calendar.YEAR) != y2);
	        }
	        return days;
	    }
	 @Test
	 public void getDaysBetweenTest(){
		 Calendar calendar1=Calendar.getInstance(); 
		 Calendar calendar2=Calendar.getInstance(); 
		 calendar1.set(2017,07,30);
		 calendar2.set(2017,07,03);
		 int days=getDaysBetween(calendar1, calendar2);
		 System.out.println("相隔"+days+"天");
	 }
}
